//
//  RecentActivityViewController.m
//  Rockval
//
//  Created by TuanNT on 4/29/14.
//  Copyright (c) 2014 TNL. All rights reserved.
//

#import "RecentActivityViewController.h"
#import "ActivityTableViewCell.h"

@interface RecentActivityViewController ()
@property (nonatomic, strong) NSArray *recentActivities;
@end

@implementation RecentActivityViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
	if (self) {
		// Custom initialization
	}
	return self;
}

- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view.
	[self setUpView];
}

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	[self.navigationController setToolbarHidden:YES animated:NO];
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}

   #pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
	// Get the new view controller using [segue destinationViewController].
	// Pass the selected object to the new view controller.
}

//================================ METHODS ================================
#pragma mark -
#pragma mark METHODS

- (void)setUpView {
	[self getRecentActivities];
}

- (void)getRecentActivities {
	NSString *customerId = [DataManager sharedInstance].account.customerId;
	[[DataManager sharedInstance] recentActivityForCustomerWithId:customerId block: ^(NSArray *list) {
	    _recentActivities = [NSArray arrayWithArray:list];
	    [self.tableView reloadData];
	}];
}

- (Activity *)activityAtIndexPatch:(NSIndexPath *)indexPath {
	return _recentActivities[indexPath.row];
}

//================================ UITABLEVIEW ================================
#pragma mark -
#pragma mark UITABLEVIEW

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSInteger count = 1;
	if (_recentActivities.count > 1) {
		count = _recentActivities.count;
	}
	return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	UITableViewCell *cell = nil;
	if (_recentActivities.count == 0) {
		cell = [tableView dequeueReusableCellWithIdentifier:@"EmptyTableViewCell"];
	}
	else {
		cell = [tableView dequeueReusableCellWithIdentifier:@"ActivityTableViewCell"];

		Activity *activity = [self activityAtIndexPatch:indexPath];
		ActivityTableViewCell *activityCell = (ActivityTableViewCell *)cell;
		[activityCell.titleLabel setText:activity.title];
		[activityCell.timeLabel setText:activity.time];
		[activityCell.thumbnailImageView setImageWithURL:[NSURL URLWithString:activity.image]
		                     usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

//        Customer = "David Krbec";
//        Description = "Property has been added";
//        Image = "http://krbecproductions.com/rockval/catalog_data/customer/5/photos/eea9207d5c.jpg";
//        Link = "http://krbecproductions.com/rockval/listing-view/8";
//        Time = "1 month(s) ago";
//        Title = "Jimmy's Place";
	}

	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
